# -*- coding: utf-8 -*-
# @Author: Tim Liu
# @Date: 2024-05-28
# @Last Modified by: Tim Liu
# @Last Modified time: 2024-05-28

# @Description: Knowledge Base Document, 
# a Document stores a file’s [source url < = > doc_id] information, with several key metadata: doc_id, source_url, source_type, kbase_id(collection_name). 
# In Crewplus, doc_id is universally unique. One Document can be linked to many Knowledge Bases, which is useful for knowledge sharing across domains.

from sqlalchemy.orm import relationship, Mapped, mapped_column
from sqlalchemy import String, Boolean, Integer, ForeignKey, Text, DateTime
from datetime import datetime

from db.db_base import BaseModel

from apps.vadmin.auth.models import VadminUser
from .task import TaskDB  # Ensure TaskDB is imported before DocumentDB

from . import KnowledgeBaseDB

class DocumentDB(BaseModel):
    __tablename__ = "crewplus_kb_document"
    __table_args__ = ({'comment': 'knowledge base document'})
    
    # document source url  
    source_url: Mapped[str] = mapped_column(Text, index=False, nullable=False, comment="Document source url")
    
    kbase_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("crewplus_knowledge_base.id", ondelete='RESTRICT'),
        comment="Knowledge Base ID"
    )
    kbase: Mapped[list["KnowledgeBaseDB"]] = relationship(foreign_keys=kbase_id)

    # Supported types: pdf, html, txt, docx; image, video, audio; filled while ingesting
    file_type: Mapped[str] = mapped_column(String(80), nullable=True, comment="Document file type")

    source_type: Mapped[str] = mapped_column(String(80), nullable=True, comment="Document source type, from sharepoint, google drive, ...")

    # Document Title, could be empty. If empty, we can generate it by llm while ingesting
    title: Mapped[str] = mapped_column(String(255), index=True, nullable=True, comment="Document Title")
    
    # Document Summary, we can retrieve it from the content by llm while ingesting 
    summary: Mapped[str] = mapped_column(Text, nullable=True, comment="Summary")
    
    # Document Content in text format, we can use it to describe an image document by mLLM while ingesting
    content: Mapped[str] = mapped_column(Text, nullable=True, comment="Content")
    
    view_number: Mapped[int] = mapped_column(Integer, default=0, comment="document view number")
    
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, comment="active flag")
    
    # ingestion status-  0: empty, 200: ingested, 240: graph built, 400: not ingested, 500: error
    ingestion_status: Mapped[int] = mapped_column(Integer, default=0, comment="ingestion status- 0: empty, 200: ingested, 240: graph built, 400: not ingested, 500: error")
    
    # ingestion time, filled while ingesting
    ingestion_start_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="ingestion start time")
    ingestion_end_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="ingestion end time")
    
    task_id: Mapped[str | None] = mapped_column(
        String(255),
        ForeignKey("crewplus_tasks.id", ondelete='RESTRICT'),
        nullable=True,  # Make task_id nullable
        comment="Task ID"
    )
    
    create_user_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("vadmin_auth_user.id", ondelete='RESTRICT'),
        comment="creator"
    )
    create_user: Mapped[VadminUser] = relationship(foreign_keys=create_user_id)
    
    download_url: Mapped[str | None] = mapped_column(Text, index=False, nullable=True, comment="Document source url")
